package mapEditor.vo
{
	import flash.events.EventDispatcher;
	
	import mapEditor.events.MapEditorEvent;
	
	import utils.DisplayUtil;

	public class ResItemVO extends EventDispatcher
	{
		private var _resXml:XML;		
		private var _id:int;
		private var _type:int;	
		private var _name:String;
		private var _keywords:String;		
		private var _gridRow:int;
		private var _gridCols:int;
		private var _items:Vector.<ResItemItemVO>=new Vector.<ResItemItemVO>();
		private var _grids:Vector.<MapTerrainTileVO>=new Vector.<MapTerrainTileVO>();
		
		public function ResItemVO(resXml:XML)
		{
			_resXml=resXml;
			if(_resXml!=null)
			{
				_id=int(_resXml.@id);
				_type=int(_resXml.@type);
				_name=_resXml.@name;
				_keywords=_resXml.@keywords;				
				_gridRow=int(_resXml.@gridRow);
				_gridCols=int(_resXml.@gridCols);
				for each(var itemXml:XML in _resXml.asset.item)
				{
					var resItemItemVO:ResItemItemVO=new ResItemItemVO();
					resItemItemVO.assetID=int(itemXml.@assetID);
					resItemItemVO.x=int(itemXml.@x);
					resItemItemVO.y=int(itemXml.@y);
					resItemItemVO.z=int(itemXml.@z);
					_items.push(resItemItemVO);
				}
				
				for each(itemXml in _resXml.grid.item)
				{
					var resItemGridVO:MapTerrainTileVO=new MapTerrainTileVO();
					resItemGridVO.y=int(itemXml.@row);
					resItemGridVO.x=int(itemXml.@cols);
					resItemGridVO.walkable=itemXml.@walk=="1";
					resItemGridVO.height=int(itemXml.@height);
					resItemGridVO.bottom=int(itemXml.@bottom);
					_grids.push(resItemGridVO);
				}
			}
		}
		
		public function addItem(resItemItemVO:ResItemItemVO):void
		{
			_items.push(resItemItemVO);
			dispatchEvent(new MapEditorEvent(MapEditorEvent.RES_ITEM_ADD_ITEM, resItemItemVO));
		}
		
		public function removeItem(resItemItemVO:ResItemItemVO):void
		{
			if(_items.indexOf(resItemItemVO)==-1)return;
			_items.splice(_items.indexOf(resItemItemVO), 1);
			dispatchEvent(new MapEditorEvent(MapEditorEvent.RES_ITEM_REMOVE_ITEM, resItemItemVO));
			resItemItemVO.dispose();
			resItemItemVO=null;
		}

		public function get id():int
		{
			return _id;
		}

		public function set id(value:int):void
		{
			_id = value;
		}

		public function get type():int
		{
			return _type;
		}

		public function set type(value:int):void
		{
			_type = value;
		}

		public function get gridRow():int
		{
			return _gridRow;
		}

		public function set gridRow(value:int):void
		{
			_gridRow = value;
		}

		public function get gridCols():int
		{
			return _gridCols;
		}

		public function set gridCols(value:int):void
		{
			_gridCols = value;
		}

		public function get items():Vector.<ResItemItemVO>
		{
			return _items;
		}

		public function get name():String
		{
			return _name;
		}

		public function set name(value:String):void
		{
			_name = value;
		}

		public function dispose():void
		{
			while(_items && _items.length>0)
			{
				DisplayUtil.dispose(_items.shift());
			}
			_items=null;
			while(_grids && _grids.length>0)
			{
				DisplayUtil.dispose(_grids.shift());
			}
			_grids=null;
			_resXml=null;
		}

		public function change():void
		{
			dispatchEvent(new MapEditorEvent(MapEditorEvent.RES_ITEM_PROPERTY_CHANGE));
		}

		public function get keywords():String
		{
			return _keywords;
		}

		public function set keywords(value:String):void
		{
			_keywords = value;
		}

		public function get grids():Vector.<MapTerrainTileVO>
		{
			return _grids;
		}

		public function set grids(value:Vector.<MapTerrainTileVO>):void
		{
			_grids = value;
		}


	}
}